package com.demo2do.fms.admin.service.data.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.data.TyreFeeDao;
import com.demo2do.fms.admin.entity.report.data.TyreFeeReportSearch;
import com.demo2do.fms.admin.handler.support.FmsJdbcSupport;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.TyreFeeService;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.business.VehicleFeature;
import com.demo2do.fms.entity.data.cost.TyreFee;

/**
 * 
 * @author Wy
 *
 */
@Service("tyreFeeService")
public class TyreFeeServiceImpl extends DataServiceImpl<TyreFee> implements TyreFeeService{
    
    @Dao
    private TyreFeeDao tyreFeeDao;
    
    @Autowired
    private FmsJdbcSupport fmsJdbcSupport;
     /*
      * (non-Javadoc)
      * @see com.demo2do.fms.admin.service.data.TyreFeeService#count(java.util.List)
      */
     public int count(List<Long> orgIds) {
         
         //if orgIds is null, return 0
         if(CollectionUtils.isEmpty(orgIds))
             return 0;
          
         return tyreFeeDao.count(orgIds);
     }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.TyreFeeService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<TyreFee> list(List<Long> orgIds, Page page) {
        
        //if orgIds is null, return null
        if(CollectionUtils.isEmpty(orgIds))
            return null;
        
        List<TyreFee> result = tyreFeeDao.list(orgIds, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.TyreFeeService#list(com.demo2do.fms.entity.company.Organization, com.demo2do.fms.entity.data.business.VehicleFeature)
     */
    public List<TyreFee> list(Organization organization, VehicleFeature vehicleFeature) {
        
        return tyreFeeDao.list(organization.getId(), vehicleFeature.getId());
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.TyreFeeService#edit(java.lang.Long, com.demo2do.fms.entity.authority.Organization, com.demo2do.fms.entity.data.business.VehicleFeature, java.util.List, java.util.List, java.util.List)
     */
    public void edit(Long operator,Organization organization, VehicleFeature vehicleFeature, List<Integer> rangeFroms, List<BigDecimal> prices) {
        
        // 1. disable all the tyre fee according to organization and vehicle_feature
        tyreFeeDao.disable(organization.getId(), vehicleFeature.getId());
        
        //2.保存页面的数据
        if(CollectionUtils.isNotEmpty(rangeFroms) && CollectionUtils.isNotEmpty(prices)){
            
            for(int i=0;i<rangeFroms.size();i++){
                
                Integer rangeFrom = rangeFroms.get(i);
                BigDecimal price = prices.get(i);
                
                Integer rangeTo = (rangeFroms.size() - 1 == i) ? -1 : (rangeFroms.get(i+1) -1);
                
                TyreFee tyreFee=new TyreFee(rangeFrom, rangeTo, price , organization , vehicleFeature);
                
                genericDaoSupport.save(tyreFee.init().operator(operator));
            }
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.TyreFeeService#exportAll(com.demo2do.fms.admin.entity.report.data.TyreFeeReportSearch)
     */
    public List<Map<String, Object>> exportAll(TyreFeeReportSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        RowMapper<Map<String, Object>> rowMapper = search.buildRowMapper();
        return fmsJdbcSupport.queryForList(sentence, parameters, rowMapper);
    }

}
